package ynu.csy.elm_springboot3.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import ynu.csy.elm_springboot3.DTO.Business;
import ynu.csy.elm_springboot3.DTO.Result;
import ynu.csy.elm_springboot3.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "business控制器 V1")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    @Operation(summary = "获取所有商家列表", description = "返回所有商家的详细信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = List.class))),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @GetMapping("/business")
    public Result list() {
        List<Business> businessList = businessService.list();
        return Result.success(businessList);
    }

    @Operation(summary = "根据商家名称搜索商家", description = "根据商家名称搜索并返回商家列表")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = List.class))),
            @ApiResponse(responseCode = "404", description = "未找到商家"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @GetMapping("/business/search/{name}")
    public Result search(@Parameter(description = "商家名称", required = true) @PathVariable String name) {
        List<Business> businessList = businessService.search(name);
        return Result.success(businessList);
    }

    @Operation(summary = "根据订单类型ID获取商家列表", description = "根据订单类型ID返回商家列表")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = List.class))),
            @ApiResponse(responseCode = "404", description = "未找到商家"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @GetMapping("/business/orderTypeId/{orderTypeId}")
    public Result getByOrderTypeId(@Parameter(description = "订单类型ID", required = true) @PathVariable Integer orderTypeId) {
        List<Business> businessList = businessService.getByOrderTypeId(orderTypeId);
        return Result.success(businessList);
    }

    @Operation(summary = "根据商家ID获取商家信息", description = "根据商家ID返回商家的详细信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Business.class))),
            @ApiResponse(responseCode = "404", description = "商家不存在"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @GetMapping("/business/businessId/{businessId}")
    public Result getByBusinessId(@Parameter(description = "商家ID", required = true) @PathVariable Integer businessId) {
        Business business = businessService.getByBusinessId(businessId);
        return Result.success(business);
    }

    @Operation(summary = "添加商家", description = "添加一个新的商家")
    @ApiResponses({
            @ApiResponse(responseCode = "201", description = "商家添加成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @PostMapping("/business")
    public Result add(@RequestBody Business business) {
        businessService.add(business);
        return Result.success();
    }

    @Operation(summary = "删除商家", description = "根据商家ID删除商家")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "404", description = "商家不存在"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @DeleteMapping("/business/{businessId}")
    public Result delete(@Parameter(description = "商家ID", required = true) @PathVariable Integer businessId) {
        businessService.delete(businessId);
        return Result.success();
    }

    @Operation(summary = "获取商家的总金额", description = "根据商家ID和用户ID获取商家的总金额")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Double.class))),
            @ApiResponse(responseCode = "404", description = "商家或用户不存在"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @GetMapping("/business/total/{businessId}/{userId}")
    public Result total(@Parameter(description = "商家ID", required = true) @PathVariable Integer businessId,
                        @Parameter(description = "用户ID", required = true) @PathVariable Long userId) {
        Double total = businessService.total(businessId, userId);
        return Result.success(total);
    }

    @Operation(summary = "获取商家的订单数量", description = "根据商家ID和用户ID获取商家的订单数量")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Integer.class))),
            @ApiResponse(responseCode = "404", description = "商家或用户不存在"),
            @ApiResponse(responseCode = "500", description = "服务器错误")
    })
    @GetMapping("/business/quantity/{businessId}/{userId}")
    public Result quantity(@Parameter(description = "商家ID", required = true) @PathVariable Integer businessId,
                           @Parameter(description = "用户ID", required = true) @PathVariable Long userId) {
        Integer quantity = businessService.getQuantity(businessId, userId);
        return Result.success(quantity);
    }
}